﻿using CCWin.SkinControl;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace MDF
{
    public partial class ProgressForm : Form
    {
        private SkinProgressBar ProgressBar2;
        private SkinProgressBar ProgressBar3;
        private CancellationTokenSource m_Cancellation;
        /// <summary>
        /// 进度窗口初始化
        /// </summary>
        public ProgressForm()
        {
            InitializeComponent();
            m_Cancellation = new CancellationTokenSource();
        }
        /// <summary>
        /// 进度窗口初始化
        /// </summary>
        /// <param name="Text">控件文本</param>
        public ProgressForm(string Text)
        {
            InitializeComponent();
            this.Label.Text = Text + "计算中";
            m_Cancellation = new CancellationTokenSource();
        }
        /// <summary>
        /// 取消执行任务
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Cancel_Button_Click(object sender, EventArgs e)
        {
            try
            {
                m_Cancellation.Cancel();
            }
            catch (Exception)
            {

                throw;
            }
        }
        /// <summary>
        /// 显示空间匹配进度
        /// </summary>
        /// <param name="Token">取消操作的对象</param>
        private void SetSpaceProgressValue(CancellationToken Token)
        {
            try
            {
                while (true)
                {
                    if (Token.IsCancellationRequested)
                    {
                        this.Invoke(new Action(() => this.Close()));
                        break;
                    }
                    else
                    {
                        if (DatasetsMatchResult.SpaceProgressValue < 100)
                        {
                            if (this.ProgressBar.InvokeRequired)
                            {
                                this.Invoke(new Action(() => this.ProgressBar.Value = DatasetsMatchResult.SpaceProgressValue));
                            }
                            else
                            {
                                ProgressBar.Value = DatasetsMatchResult.SpaceProgressValue;
                            }
                        }
                        else
                        {
                            this.Invoke(new Action(() => this.DialogResult = DialogResult.OK));
                            break;
                        }
                    }
                }

            }
            catch (Exception)
            {

                throw;
            }
        }
        /// <summary>
        /// 显示属性匹配进度
        /// </summary>
        /// <param name="Token">取消操作的对象</param>
        private void SetAttributeProgressValue(CancellationToken Token)
        {
            try
            {
                while (true)
                {
                    if (Token.IsCancellationRequested)
                    {
                        this.Invoke(new Action(() => this.Close()));
                        break;
                    }
                    else
                    {
                        if (DatasetsMatchResult.AttributeProgressValue < 100)
                        {
                            if (this.ProgressBar.InvokeRequired)
                            {
                                this.Invoke(new Action(() => this.ProgressBar.Value = DatasetsMatchResult.AttributeProgressValue));
                            }
                            else
                            {
                                ProgressBar.Value = DatasetsMatchResult.AttributeProgressValue;
                            }
                        }
                        else
                        {
                            this.Invoke(new Action(() => this.DialogResult = DialogResult.OK));
                            break;
                        }
                    }
                }

            }
            catch (Exception)
            {

                throw;
            }
        }
        /// <summary>
        /// 显示拓扑匹配进度
        /// </summary>
        /// <param name="Token">取消操作的对象</param>
        private void SetTopologyProgressValue(CancellationToken Token)
        {
            try
            {
                while (true)
                {
                    if (Token.IsCancellationRequested)
                    {
                        this.Invoke(new Action(() => this.Close()));
                        break;
                    }
                    else
                    {
                        if (DatasetsMatchResult.TopollogyProgressValue < 100)
                        {
                            if (this.ProgressBar.InvokeRequired)
                            {
                                this.Invoke(new Action(() => this.ProgressBar.Value = DatasetsMatchResult.TopollogyProgressValue));
                            }
                            else
                            {
                                ProgressBar.Value = DatasetsMatchResult.TopollogyProgressValue;
                            }
                        }
                        else
                        {
                            this.Invoke(new Action(() => this.DialogResult = DialogResult.OK));
                            break;
                        }
                    }
                }

            }
            catch (Exception)
            {

                throw;
            }
        }
        /// <summary>
        /// 显示融合进度
        /// </summary>
        /// <param name="Token">取消操作的对象</param>
        private void SetFusionProgressValue(CancellationToken Token)
        {
            try
            {
                while (true)
                {
                    if (Token.IsCancellationRequested)
                    {
                        this.Invoke(new Action(() => this.Close()));
                        break;
                    }
                    else
                    {
                        if (DatasetsMatchResult.FusionProgressValue < 100)
                        {
                            if (this.ProgressBar.InvokeRequired)
                            {
                                this.Invoke(new Action(() => this.ProgressBar.Value = DatasetsMatchResult.FusionProgressValue));
                            }
                            else
                            {
                                ProgressBar.Value = DatasetsMatchResult.FusionProgressValue;
                            }
                        }
                        else
                        {
                            this.Invoke(new Action(() => this.DialogResult = DialogResult.OK));
                            break;
                        }
                    }
                }

            }
            catch (Exception)
            {

                throw;
            }
        }
        /// <summary>
        /// 显示模式匹配进度
        /// </summary>
        /// <param name="Token">取消操作的对象</param>
        private void SetModeMatchProgressValue(CancellationToken Token)
        {
            try
            {
                while (true)
                {
                    if (Token.IsCancellationRequested)
                    {
                        this.Invoke(new Action(() => this.Close()));
                        break;
                    }
                    else
                    {
                        if (DatasetsMatchResult.SpaceProgressValue < 100 || DatasetsMatchResult.AttributeProgressValue < 100 || DatasetsMatchResult.TopollogyProgressValue < 100)
                        {
                            if (this.ProgressBar.InvokeRequired)
                            {
                                this.Invoke(new Action(() => this.ProgressBar.Value = DatasetsMatchResult.SpaceProgressValue));
                                this.Invoke(new Action(() => this.ProgressBar2.Value = DatasetsMatchResult.AttributeProgressValue));
                                this.Invoke(new Action(() => this.ProgressBar3.Value = DatasetsMatchResult.TopollogyProgressValue));
                            }
                            else
                            {
                                ProgressBar.Value = DatasetsMatchResult.SpaceProgressValue;
                                ProgressBar2.Value = DatasetsMatchResult.AttributeProgressValue;
                                ProgressBar3.Value = DatasetsMatchResult.TopollogyProgressValue;
                            }
                        }
                        else
                        {
                            this.Invoke(new Action(() => this.DialogResult = DialogResult.OK));
                            break;
                        }
                    }
                }

            }
            catch (Exception)
            {

                throw;
            }
        }
        /// <summary>
        /// 窗体展示后进行，确保窗体正确显示
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void ProgressForm_Shown(object sender, EventArgs e)
        {
            try
            {
                this.Update();
                Task task;
                switch (this.Text)
                {
                    case "KJPP":
                        task = new Task(() => SetSpaceProgressValue(m_Cancellation.Token), m_Cancellation.Token);
                        break;
                    case "SXPP":
                        task = new Task(() => SetAttributeProgressValue(m_Cancellation.Token), m_Cancellation.Token);
                        break;
                    case "TPPP":
                        task = new Task(() => SetTopologyProgressValue(m_Cancellation.Token), m_Cancellation.Token);
                        break;
                    case "SJRH":
                        this.Label.Text = "数据融合中";
                        task = new Task(() => SetFusionProgressValue(m_Cancellation.Token), m_Cancellation.Token);
                        break;
                    default:
                        //更新窗体样式，
                        ChangeForm();
                        task = new Task(() => SetModeMatchProgressValue(m_Cancellation.Token), m_Cancellation.Token);
                        break;
                }
                task.Start();
            }
            catch (Exception)
            {

                throw;
            }
        }
        /// <summary>
        /// 修改窗体样式
        /// </summary>
        private void ChangeForm()
        {
            try
            {
                //修改界面
                this.Size = new Size(283, 215);
                this.Cancel_Button.Location = new Point(97, 184);
                this.Label.Text = "空间匹配";
                //新建控件
                Label label2 = new Label()
                {
                    Name = "label2",
                    Text = "属性匹配",
                    AutoSize = true,
                    Size = new Size(35, 14),
                    Location = new Point(13, 70),
                    TextAlign = ContentAlignment.MiddleCenter
                };
                Label label3 = new Label()
                {
                    Name = "label3",
                    Text = "拓扑匹配",
                    AutoSize = true,
                    Size = new Size(35, 14),
                    Location = new Point(13, 126),
                    TextAlign = ContentAlignment.MiddleCenter
                };
                ProgressBar2 = new SkinProgressBar()
                {
                    Back = null,
                    BackColor = Color.Transparent,
                    BarBack = null,
                    BarBackStyle = BackStyle.Stretch,
                    BarRadius = 6,
                    BarRadiusStyle = CCWin.SkinClass.RoundStyle.All,
                    Border = Color.SkyBlue,
                    Font = new Font("宋体", 9F, FontStyle.Regular, GraphicsUnit.Point, ((byte)(134))),
                    ForeColor = Color.Black,
                    FormatString = "{0:0%}",
                    InnerBorder = Color.AliceBlue,
                    Location = new Point(12, 93),
                    Name = "ProgressBar2",
                    RadiusStyle = CCWin.SkinClass.RoundStyle.All,
                    Size = new Size(259, 21),
                    TabIndex = 5,
                    TrackBack = Color.DeepSkyBlue,
                    TrackFore = Color.DeepSkyBlue
                };
                ProgressBar3 = new SkinProgressBar()
                {
                    Back = null,
                    BackColor = Color.Transparent,
                    BarBack = null,
                    BarBackStyle = BackStyle.Stretch,
                    BarRadius = 6,
                    BarRadiusStyle = CCWin.SkinClass.RoundStyle.All,
                    Border = Color.SkyBlue,
                    Font = new Font("宋体", 9F, FontStyle.Regular, GraphicsUnit.Point, ((byte)(134))),
                    ForeColor = Color.Black,
                    FormatString = "{0:0%}",
                    InnerBorder = Color.AliceBlue,
                    Location = new Point(12, 149),
                    Name = "ProgressBar3",
                    RadiusStyle = CCWin.SkinClass.RoundStyle.All,
                    Size = new Size(259, 21),
                    TabIndex = 5,
                    TrackBack = Color.DeepSkyBlue,
                    TrackFore = Color.DeepSkyBlue
                };
                //添加到窗体
                this.Controls.Add(label2);
                this.Controls.Add(ProgressBar2);
                this.Controls.Add(label3);
                this.Controls.Add(ProgressBar3);
                //更新
                this.Update();
            }
            catch (Exception)
            {

                throw;
            }
        }
    }
}
